package com.timmy.test1;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ScreenUtils;
import com.timmy.test1.common.JiActivityVKeyHelper;
import com.timmy.test1.view.CustomRoundView;
import com.timmy.test1.view.JKeyBoardView;
import com.timmy.test1.view.KeyView;

public class KeyTestActivity extends AppCompatActivity {


    private View fl_guide_root;

    private View fl_root;

    private View                 fl_vkey;
    private JiActivityVKeyHelper mJiActivityVKeyHelper;

    private View          layout_keyboard;
    private JKeyBoardView keyboard;
    private int mScreenWidth;
    private int mScreenHeight;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_key_test);
        ScreenUtils.setLandscape(this);
        ScreenUtils.setFullScreen(this);
        initView();
    }

    private void initView() {
        fl_root = findViewById(R.id.fl_root);
        this.fl_vkey = findViewById(R.id.fl_vkey);

        this.layout_keyboard = findViewById(R.id.layout_keyboard);

        fl_root.post(new Runnable() {
            @Override
            public void run() {
                mScreenWidth = fl_root.getWidth();
                mScreenHeight = fl_root.getHeight();
                mJiActivityVKeyHelper = new JiActivityVKeyHelper(KeyTestActivity.this, fl_vkey, mScreenWidth, mScreenHeight);
                mJiActivityVKeyHelper.ll_vkey_review.setVisibility(View.VISIBLE);

                mJiActivityVKeyHelper.setKeyBoardListener(new JiActivityVKeyHelper.KeyBoardListener() {
                    @Override
                    public void onImportKeyBgImage() {
                    }

                    @Override
                    public void onVisibilityGone() {
                        layout_keyboard.setVisibility(View.GONE);
                        keyboard.setVisibility(View.GONE);
                    }

                    @Override
                    public void onVisibilityVisible() {
                        layout_keyboard.setVisibility(View.VISIBLE);
                        keyboard.setVisibility(View.VISIBLE);

                    }
                });

            }
        });

        this.keyboard = findViewById(R.id.keyboard);
        keyboard.setJKeyListener(new JKeyBoardView.JKeyListener() {
            @Override
            public void onJKeyDown(KeyView param1KeyView) {

            }

            @Override
            public void onJKeyUp(KeyView param2KeyView) {
                if (param2KeyView.getText().equals("隐藏")) {
                    layout_keyboard.setVisibility(View.GONE);
                    keyboard.setVisibility(View.GONE);
                    return;
                }

                // 创建view
                CustomRoundView customRoundView = new CustomRoundView((Context) KeyTestActivity.this);
                customRoundView.setText(param2KeyView.getText());
                customRoundView.setTextColor(ContextCompat.getColor(KeyTestActivity.this, R.color.white));
                customRoundView.setGravity(Gravity.CENTER);
                customRoundView.setTypeface(Typeface.DEFAULT_BOLD);
                customRoundView.setScanCode(param2KeyView.getScanCode());
                customRoundView.setLongPress(false);

                if (param2KeyView.getText().equals("Space")) {
                    customRoundView.setRound(false);
                    mJiActivityVKeyHelper.addCustomRoundButton(customRoundView);
                    customRoundView.setBackground(KeyTestActivity.this.getResources().getDrawable(R.mipmap.key_space_normal));
                    customRoundView.setNormalBackground(R.mipmap.key_space_normal);
                    customRoundView.setPressBackground(R.mipmap.key_space_press);
                    customRoundView.vWidth = 20.0F;
                    customRoundView.vHeight = 5.0F;
                    int i = (int) (customRoundView.vWidth / 100.0D * mScreenWidth);
                    int j = (int) (customRoundView.vHeight / 100.0D * mScreenWidth);
                    ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) customRoundView.getLayoutParams();
                    marginLayoutParams.width = i;
                    marginLayoutParams.height = j;
                    customRoundView.setLayoutParams(marginLayoutParams);
                    return;
                }

                customRoundView.setBackground(KeyTestActivity.this.getResources().getDrawable(R.mipmap.key_char_normal));
                customRoundView.setNormalBackground(R.mipmap.key_char_normal);
                customRoundView.setPressBackground(R.mipmap.key_char_press);
                mJiActivityVKeyHelper.addCustomRoundButton(customRoundView);
            }
        });
    }
}
